package com.gr100.biz.api.user;

import com.gr100.biz.api.bo.MetaObject;
import com.jfinal.aop.Before;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbPro;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;

import java.util.List;

/**
 * Created by james on 16/3/16.
 */
public class UserModel {
    private static final Log logger = Log.getLog("UserModel");
    private static UserModel ourInstance = new UserModel();

    public static UserModel me() {
        return ourInstance;
    }

    private UserModel() {
    }


    DbPro api = Db.use("api");
    DbPro ec = Db.use("ec");
    DbPro erp = Db.use("erp");

    public MetaObject find(User u){
        String type = u.getType();
        String loginName = u.getLoginName();
        String loginPass = u.getLoginPass();

        String sql = "select a.id,a.balance,a.point,a.nickname,a.level,a.lastlogintime,a.headimgurl,b.openid,a.objectId,a.createAt,a.updateAt,a.status from gr_login_info a";
        if("1".equals(type)){
            sql += " where a.status = 1 and a.phone = '"+loginName+"' and a.login_pass = '"+loginPass+"'";
        }else if("2".equals(type)){
            sql += " where a.status = 1 and a.email = '"+loginName+"' and a.login_pass = '"+loginPass+"'";
        }else if("3".equals(type)){
            sql += ",gr_thirdparty b where a.status = 1 and a.id = b.uid and b.openidType = 'WX' and b.openid = '"+loginName+"'";
        }else if("4".equals(type)){
            sql += ",gr_thirdparty b where a.status = 1 and a.id = b.uid and b.openidType = 'QQ' and b.openid = '"+loginName+"'";
        }else if("5".equals(type)){
            sql += " where a.status = 1 and a.login_name = '"+loginName+"' and a.login_pass = '"+loginPass+"'";
        }
        logger.info(sql);

        Record r = api.findFirst(sql);

        if(r==null){
            return null;
        }else{
            return new MetaObject(r.getColumns());
        }
    }

    /**
     * 验证用户是否已经存在
     * @param u
     * @return
     */
    public boolean checkExists(User u){

        String type = u.getType();
        String loginName = u.getLoginName();

        String sql = "select a.id from gr_login_info a";
        if(User.LOGIN_TYPE.PHONE.equal(type)){
            sql += " where a.status = 1 and a.phone = '"+loginName+"'";
        }else if(User.LOGIN_TYPE.EMAIL.equal(type)){
            sql += " where a.status = 1 and a.email = '"+loginName+"'";
        }else if(User.LOGIN_TYPE.WX.equal(type)){
            sql += ",gr_thirdparty b where a.status = 1 and a.id = b.uid and b.openidType = 'WX' and b.openid = '"+loginName+"'";
        }else if(User.LOGIN_TYPE.QQ.equal(type)){
            sql += ",gr_thirdparty b where a.status = 1 and a.id = b.uid and b.openidType = 'QQ' and b.openid = '"+loginName+"'";
        }else if(User.LOGIN_TYPE.USERNAME.equal(type)){
            sql += " where a.status = 1 and a.login_name = '"+loginName+"'";
        }

        List<Record> list = api.find(sql);

        if(list.size()>0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 保存用户
     * @param u
     * @return
     */
    @Before(Tx.class)
    public int save(User u){

        String type = u.getType();
        String loginName = u.getLoginName();
        String loginPass = u.getLoginPass();
        String unionID = u.getUnionID();

        Record grLoginInfo = new Record();
        String openidType = "";

        if(User.LOGIN_TYPE.PHONE.equal(type)){
            grLoginInfo.set("phone", loginName);
        }else if(User.LOGIN_TYPE.EMAIL.equal(type)){
            grLoginInfo.set("email", loginName);
        }else if(User.LOGIN_TYPE.WX.equal(type)){
            openidType = "WX";
        }else if(User.LOGIN_TYPE.QQ.equal(type)){
            openidType = "QQ";
        }else if(User.LOGIN_TYPE.USERNAME.equal(type)){
            grLoginInfo.set("login_name", loginName);
        }
        grLoginInfo.set("login_pass", loginPass);
        grLoginInfo.set("level", 1);
        grLoginInfo.set("createAt", System.currentTimeMillis());
        grLoginInfo.set("updateAt", System.currentTimeMillis());

        boolean flag = api.save("gr_login_info", grLoginInfo);

        int uid = Integer.parseInt(grLoginInfo.get("id").toString());

        //如果是第三方注册，要往thirdparty表里插入数据
        if(User.LOGIN_TYPE.WX.equal(type) || User.LOGIN_TYPE.QQ.equal(type)){
            Record thirdparty = new Record();
            thirdparty.set("uid", uid);
            thirdparty.set("openid", loginName);
            thirdparty.set("unionID", unionID);
            thirdparty.set("openidType", openidType);
            thirdparty.set("createAt", System.currentTimeMillis());
            thirdparty.set("updateAt", System.currentTimeMillis());
            api.save("gr_thirdparty", thirdparty);
        }
        return uid;
    }

}
